import {
  request,
  PyRequest,
  MapRequest
} from './request'


// 添加访客申请 
export function addRecordApi(data) {
  return request({
    url: '/addRecord',
    data: data,
    method: 'post'
  })
}




// 添加到python数据库 header: {'Content-Type': 'multipart/form-data'}
export function uploadFacePy(data) {
  return PyRequest({
    url: '/face/weixin_insert',
    data: data,
    method: 'post',
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}



// 查看申请记录
export function getRecordApi(id) {
  return request({
    url: '/getRecordsById/' + id,
    method: 'get'
  })
}


//查询所有地标
export function getAllLandmarkApi() {
  return request({
    url: '/landmark/getAll',
    method: 'get'
  })
}

// 查看美食GET获取所有菜品 
export function getFoodApi() {
  return request({
    url: '/cafeteria/getAll',
    method: 'get'
  })
}


//
export function getList(data) {
  return request({
    url: '/upload',
    header: {
      'content-type': 'application/json'
      // 如有token的需求可以在此处携带，也可以在request.js定义
    },
    data: data,
    method: 'post'
  })
}

//GET {id}
//根据ID查询 地点
export function getByIdApi(id) {
  return request({
    url: '/landmark/getById/' + id,
    method: 'get'
  })
}

//距离计算

export function getDistanceApi(params) {
console.log(params.from.longitude);

//  return MapRequest({
//     url: '/ws/direction/v1/walking/?from=' 
//     + params.from.latitude + "," + params.from.longitude + '&to=' 
//     + params.to.latitude + "," + params.to.longitude +'&output=json&callback=cb&key='+ params.key,
//     method: 'get'
//   })
 return calculateDistance(params)
 
}

function calculateDistance(params) {
  // 解析经纬度
   var lat1 = params.from.latitude;
  var lon1 = params.from.longitude;
  var lat2 = params.from.latitude;
  var lon2 = params.from.longitude;

  // 将经纬度转换为弧度
  var radLat1 = lat1 * Math.PI / 180;
  var radLon1 = lon1 * Math.PI / 180;
  var radLat2 = lat2 * Math.PI / 180;
  var radLon2 = lon2 * Math.PI / 180;

  // 地球半径（单位：千米）
  var radius = 6371.0;

  // 使用 Haversine formula 计算距离
  var dLon = radLon2 - radLon1;
  var dLat = radLat2 - radLat1;
  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(radLat1) * Math.cos(radLat2) *
    Math.sin(dLon / 2) * Math.sin(dLon / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var distance = radius * c;

  return distance;
}





function findShortestPath(distances) {
  const cities = new Set();
  distances.forEach(({
    from,
    to
  }) => {
    cities.add(from);
    cities.add(to);
  });

  let shortestDistance = Infinity;
  let shortestPath = [];

  function dfs(currentCity, visited, path, totalDistance) {
    visited.add(currentCity);
    path.push(currentCity);

    if (visited.size === cities.size) {
      if (totalDistance < shortestDistance) {
        shortestDistance = totalDistance;
        shortestPath = [...path];
      }
    } else {
      distances.forEach(({
        from,
        to,
        distance
      }) => {
        if (from === currentCity && !visited.has(to)) {
          dfs(to, new Set(visited), [...path], totalDistance + distance);
        } else if (to === currentCity && !visited.has(from)) {
          dfs(from, new Set(visited), [...path], totalDistance + distance);
        }
      });
    }
  }

  dfs(0, new Set(), [], 0);

  return {
    path: shortestPath,
    distance: shortestDistance
  };
}


function initData(distances, currentDistances) {
  for (const {
      from,
      to,
      distance
    } of currentDistances) {
    distances.push({
      from,
      to,
      distance
    });
  }
}

export function getDisOrder(distances, currentDistances) {
  initData(distances, currentDistances)
  console.log(distances);

  const res = findShortestPath(distances);
  console.log(res.path);
  return res
}